package com.study.talk.ui.app.group.info

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.study.talk.data.model.GroupInfo
import com.study.common.ToastUtil
import com.study.talk.ui.app.base.BaseViewModel
import com.study.talk.data.model.GroupUserInfo
import com.study.talk.data.model.UserInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class GroupInfoViewModel @Inject constructor(application: Application) :
    BaseViewModel(application) {

    private val _groupDetails = MutableLiveData<GroupInfo>()
    val groupDetails: LiveData<GroupInfo> get() = _groupDetails

    private val _isMember = MutableLiveData(1)
    val isMember: LiveData<Int> get() = _isMember

    private val _mainUser = MutableLiveData<UserInfo>()
    val mainUser: LiveData<UserInfo> get() = _mainUser

    private val _members = MutableLiveData<List<GroupUserInfo>>()
    val members: LiveData<List<GroupUserInfo>> get() = _members

    fun getGroupInfo(groupId: Int) {
        viewModelScope.launch {
            val result = groupRepository.requestGroupInfo(groupId, fail = {
                showToast(it)
            })
            if (result == null) {
                showToast("群聊信息获取失败")
            } else {
                result.let { _groupDetails.postValue(it) }
                //获取群聊成员信息
                getMembers(groupId = result.id)

                //获取群主信息
                getMainUser(userId = result.mainUser)

                if (result.mainUser == app.localUserId) {
                    changeState(3)
                } else {
                    //如果已经在群里中，则不显示加入群聊按钮
                    inGroup(result.id)
                }
            }
        }
    }

    private fun changeState(state: Int) {
        _isMember.postValue(state)
    }

    private fun getMainUser(userId: Int) {
        viewModelScope.launch {
            val result = userRepository.getUserInfo(userId)
            if (result == null) {
                ToastUtil.showToast(app, "获取群主信息失败")
            } else {
                result.let { _mainUser.postValue(it) }
            }
        }
    }

    private fun getMembers(groupId: Int) {
        viewModelScope.launch {
            groupRepository.getMembers(groupId = groupId, success = {
                _members.postValue(it)
            }, fail = {
                ToastUtil.showToast(app, it)
            })
        }
    }

    fun exitGroup(groupId: Int) {
        viewModelScope.launch {
            groupRepository.exitGroup(groupId = groupId, success = {
                changeState(2)
                getMembers(groupId)
            }, fail = {
                ToastUtil.showToast(app, it)
            })
        }
    }

    fun disbandGroup(groupId: Int) {
        viewModelScope.launch {
            groupRepository.disbandGroup(groupId, success = {
                changeState(4)
            }, fail = {
                ToastUtil.showToast(app, it)
            })
        }
    }

    fun inGroup(groupId: Int) {
        viewModelScope.launch {
            groupRepository.inGroup(groupId = groupId, userId = app.localUserId, success = { b ->
                changeState(if (b) 1 else 2)
            }, fail = { msg ->
                ToastUtil.showToast(app, msg)
            })
        }
    }

}